package main.controller;

import java.util.List;

import javax.annotation.Resource;

import main.entity.ChannelVO;
import main.entity.ModuleVO;
import main.entity.ThreadsVO;
import main.entity.VoteVO;
import main.service.ChannelService;
import main.service.ModuleService;
import main.service.ThreadsService;
import main.service.VoteService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/")
public class ModuleCtrl extends BaseCtrl<ModuleVO> {

	@Autowired
	private ModuleService moduleService;
	@Autowired
	@Qualifier("channelService")
	private ChannelService channelService;
	@Resource(name="threadsService")
	private ThreadsService threadsService;
	@Resource(name="voteService")
	private VoteService voteService;
	
	@RequestMapping("index.do")
	public ModelAndView getAllModules(){
		List<ModuleVO> allModules = moduleService.loadAll(ModuleVO.class);
//		request.setAttribute("allModules", allModules);
		targetURL = "index";
		mdv.setViewName(targetURL);
		mdv.addObject("modules", allModules);
		return mdv;
	}
	
	@RequestMapping("module/{mID}/read.do")
	public ModelAndView getChannelByID(@PathVariable("mID")Integer mID){
		List<ChannelVO> channels = channelService.getByModuleID(mID);
//		request.setAttribute("allModules", allModules);
//		targetURL = "index";
		String targetURL = "";
		switch (mID) {
		case 0:
			targetURL="discuss";
			List<ThreadsVO> threadsInChannel0 = threadsService.getByChannelID(0);
			List<ThreadsVO> threadsInChannel1 = threadsService.getByChannelID(1);
			mdv.addObject("threadsInChannel0", threadsInChannel0);
			mdv.addObject("threadsInChannel1", threadsInChannel1);
			break;
		case 1:
			targetURL="feedback";
			List<VoteVO> votes = voteService.getBaseDAO().loadAll(VoteVO.class);
			mdv.addObject("votes", votes);
			break;
		case 2:
			targetURL="exam";
			break;
		case 3:
			targetURL="my_zone";
			break;
		default:
			break;
		}
		mdv.setViewName(targetURL);
		mdv.addObject("channels", channels);
		return mdv;
	}
	
	@RequestMapping("/crossGo.do")
	@ResponseBody
	public Integer cross(@RequestParam Integer vs){
		vs++;
		System.out.println(vs);
		return vs;
	}

}
